Java: "cannot find symbol" error of a String[] defined within a while-loop

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-04-17T17:33:17Z Indexed on 2010/04/17 17:43 UTC
Read the original article Hit count: 147

Filed under:
|
|
|
|

Here's the relevant code:

public static String[] runTeams (String CPUcolor) 
{  
    boolean z = false ; 
    //String[] a = new String[6] ; 
    boolean CPU = false ; 
    while (z == false) 
    { 
        while (CPU==false) 
        {
            String[] a = assignTeams () ; 
            printOrder (a) ;

            for (int i = 1; i<a.length; i++) 
            { 
                if (a[i].equals(CPUcolor)) CPU = true ;
            }
            if (CPU==false) 
            {
                System.out.println ("ERROR YOU NEED TO INCLUDE THE COLOR OF THE CPU IN THE TURN ORDER") ; 
            }
        }
        System.out.println ("is this turn order correct? (Y/N)") ; 
        String s = getIns () ; 
        while (!((s.equals ("y")) || (s.equals ("Y")) || (s.equals ("n")) || (s.equals ("N")))) 
        {
            System.out.println ("try again") ; 
            s = getIns () ; 
        } 
        if (s.equals ("y") || s.equals ("Y") ) 
        z = true ; 
    } 
    return a ; 
}

the error i get is:

Risk.java:416: cannot find symbol
symbol  : variable a
location: class Risk
        return a ; 
               ^

Why did i get this error? It seems that a is clearly defined in the line String[] a = assignTeams () ; and if anything is used by the lineprintOrder (a) ;` it seems to me that if the symbol a really couldn't be found then the compiler should blow up there and not at the return statment.

(also the method assignTeams returns an array of Strings.)

© Stack Overflow or respective owner

Related posts about java

Related posts about symbols